package org.sifebint.backend.dao.datasourcedescription;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.sifebint.backend.model.DataSourceDescription;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

public class DataSourceDescriptionDaoImpl extends SimpleJdbcDaoSupport
    implements DataSourceDescriptionDao {

  private String queryCreateDataSource;
  
  private String queryGetDataSourceDescription;
  
  private String queryGetDataSourceDescriptions;
  
  private String queryUpdateDataSource;

  private String queryRemoveDataSourceDescription;
  
  @Override
  public void createDataSourceDescription(
      DataSourceDescription dataSourceDescription) {

    getSimpleJdbcTemplate().update(
        queryCreateDataSource, 
        new Object[] {
            dataSourceDescription.getName(),
            dataSourceDescription.getJdbcDriver(),
            dataSourceDescription.getUrl(),
            dataSourceDescription.getUsername(),
            dataSourceDescription.getPassword() });

  }

  @Override
  public DataSourceDescription getDataSourceDescription(long id) {
    
    return getSimpleJdbcTemplate().queryForObject(
        queryGetDataSourceDescription,
        new ParameterizedRowMapper<DataSourceDescription>() {
          @Override
          public DataSourceDescription mapRow(ResultSet rs, int rowNum) throws SQLException {
            DataSourceDescription dataSourceDescription = new DataSourceDescription();
            dataSourceDescription.setId(rs.getLong("id"));
            dataSourceDescription.setJdbcDriver(rs.getString("jdbc_driver"));
            dataSourceDescription.setUrl(rs.getString("jdbc_url"));
            dataSourceDescription.setUsername(rs.getString("username"));
            dataSourceDescription.setPassword(rs.getString("password"));
            dataSourceDescription.setName(rs.getString("name"));
            return dataSourceDescription;
          }
        },
        new Object[] { id });
  }
  
  @Override
  public List<DataSourceDescription> getDataSourcesDescriptions() {
    
    return getSimpleJdbcTemplate().query(
        queryGetDataSourceDescriptions,
        new ParameterizedRowMapper<DataSourceDescription>() {
          @Override
          public DataSourceDescription mapRow(ResultSet rs, int rowNum) throws SQLException {
            DataSourceDescription dataSourceDescription = new DataSourceDescription();
            dataSourceDescription.setId(rs.getLong("id"));
            dataSourceDescription.setJdbcDriver(rs.getString("jdbc_driver"));
            dataSourceDescription.setUrl(rs.getString("jdbc_url"));
            dataSourceDescription.setUsername(rs.getString("username"));
            dataSourceDescription.setPassword(rs.getString("password"));
            dataSourceDescription.setName(rs.getString("name"));
            return dataSourceDescription;
          }
        },
        new Object[] {});
    
  }

  @Override
  public void updateDataSourceDescription(
      DataSourceDescription dataSourceDescription) {
    
    getSimpleJdbcTemplate().update(
        queryUpdateDataSource,
        new Object[] { 
            dataSourceDescription.getName(),
            dataSourceDescription.getJdbcDriver(),
            dataSourceDescription.getUrl(),
            dataSourceDescription.getUsername(),
            dataSourceDescription.getPassword(),
            dataSourceDescription.getId()
        });

  }
  
  @Override
  public void removeDataSourceDescription(long dataSourceDescriptionId) {
    getSimpleJdbcTemplate().update(
        queryRemoveDataSourceDescription,
        new Object[] { dataSourceDescriptionId });
  }
  
  @Required
  public void setQueryCreateDataSource(String queryCreateDataSource) {
    this.queryCreateDataSource = queryCreateDataSource;
  }

  @Required
  public void setQueryGetDataSourceDescription(
      String queryGetDataSourceDescription) {
    this.queryGetDataSourceDescription = queryGetDataSourceDescription;
  }
  
  @Required
  public void setQueryGetDataSourceDescriptions(
      String queryGetDataSourceDescriptions) {
    this.queryGetDataSourceDescriptions = queryGetDataSourceDescriptions;
  }
  
  @Required
  public void setQueryUpdateDataSource(String queryUpdateDataSource) {
    this.queryUpdateDataSource = queryUpdateDataSource;
  }
  
  @Required
  public void setQueryRemoveDataSourceDescription(String queryRemoveDataSourceDescription) {
    this.queryRemoveDataSourceDescription = queryRemoveDataSourceDescription;
  }
  
}
